import { bind } from '@/services/login';
import { companies } from '@/services/user';
import { Modal, Toast } from 'antd-mobile';
import checkCompany from './checkCompany';
const prompt = Modal.prompt;

export default function judgebind(ifs, dispatch) {
  let token = localStorage.getItem('TOKENS');
  let curcom = localStorage.getItem('companies'),
    continues = curcom && curcom != 'undefiend'; //已经选择过

  if (ifs) {
    prompt(
      '绑定您的手机号',
      '请绑定您在本公司预留的手机号码,如忘记号码请联系:13338072099',
      [
        {
          text: '取消',
          onPress: (value) => {
            Toast.info('取消绑定将无法查询到您的公司', 1);
          },
        },
        {
          text: '提交',
          onPress: (value) =>
            new Promise((resolve, reject) => {
              bind({ phone: value, open_id: token }).then((result) => {
                if (result.code != 0) {
                  Toast.info(result.message, 1);
                  reject();
                } else {
                  checkCompany(continues, dispatch);
                  resolve();
                }
              });
            }),
        },
      ],
      'default',
      null,
      ['请输入您的手机号'],
    );
  } else {
    checkCompany(continues, dispatch);
  }
}
